/*场景标签管理-----------------------------------------------------------------------------*/
function goods_listLable1(classNum) {
    var user_data = {
        lable_level: classNum
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null || data.data == "") {
                $("#lableSet1").append('<li class="clearfix pl-10 mb-5">无数据</li>');
                $("#lableSet1").append('<li id="goodsLableInp1" style="display: none" class="clearfix pl-10 mb-5"><div class="pd-5"><input name="label01" type="radio" value="" class="radio" /><label class="pl-10" id="goodsLabel1"></label></div><div class="class-input"><input type="text" autofocus class="input-text radius f-l firstLableId1" maxlength="15" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addLableClass(1,0,\'.firstLableId1\')" class="ml-10">确认</a><a href="##" onclick="celLableFirstAdd(\'#goodsLableInp1\')" class="ml-10">取消</a></div></div></li>')
            } else {
                $("#lableSet1").append('<li id="goodsLableInp1" style="display: none" class="clearfix pl-10 mb-5"><div class="pd-5"><input name="label01" type="radio" value="" class="radio" /><label class="pl-10" id="goodsLabel1"></label></div><div class="class-input"><input type="text" autofocus class="input-text radius f-l firstLableId1" maxlength="15" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addLableClass(1,0,\'.firstLableId1\')" class="ml-10">确认</a><a href="##" onclick="celLableFirstAdd(\'#goodsLableInp1\')" class="ml-10">取消</a></div></div></li>')
                for(var i = 0; i < data.data.length; i++) {

/*
                    $("#lableSet1").append('<li class="clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="goods_listLable2(2,this.value)" name="label01" type="radio" value="' + data.data[i].lable_id + '" class="radio" /><label class="pl-10"><span class="lablecode">' + data.data[i].lable_code + '</span> <input title="' + data.data[i].lable_name + '" type="text" value="' + data.data[i].lable_name + '" disabled style="height: 24px;width: 145px;border: 0px;background: white;margin-top: -4px"></label></div><div class="f-r"><a onclick="addLabelInp(this,\'#goodsLableInp2\',2,\'' + data.data[i].lable_level + '\',\'' + data.data[i].lable_code + '\',\'' + data.data[i].lable_id + '\')" class="Hui-iconfont icon-edit mr-10 powerButton 408">&#xe600;</a><a onclick="eidtLableInp(\'#goodsLableInp1\',\'#goodsLabel1\',' + data.data[i].lable_code + ',\'.firstLableId1\',\'' + data.data[i].lable_name + '\',\'' + data.data[i].lable_id + '\')" class="Hui-iconfont icon-edit mr-10 powerButton 409">&#xe6df;</a><a href="##" onclick="deleteLableClass(' + data.data[i].lable_id + ',1)" class="Hui-iconfont icon-edit mr-10 powerButton 410">&#xe6e2;</a></div></li>')
*/
                    //无删除新增编辑按钮
                    //只展示启用状态下
                    if(data.data[i].status==0){
                        $("#lableSet1").append('<li class="clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="goods_listLable2(2,this.value)" name="label01" type="radio" value="' + data.data[i].lable_id + '" class="radio" /><label class="pl-10"> <input title="' + data.data[i].lable_name + '" type="text" value="' + data.data[i].lable_name + '" disabled style="height: 24px;width: 145px;border: 0px;background: white;margin-top: -4px"></label></div></li>')
                    }

                }
            }
        }
        power();
    };
    return web.doAjax(root.goodsInter.goods_listLable, 'get', user_data, succCallBack);
}

goods_listLable1(1);

function goods_listLable2(classNum, labelId,isShowInput) {
    $(".goodsLableRemove2").remove();
    var user_data = {
        // lable_level: classNum,
        lable_id: labelId
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            if(data.data.length == 0) {
                $("#lableSet2").append('<li class="goodsLableRemove2 clearfix pl-10 mb-5">无数据</li>');
                $("#lableSet2").append('<li id="goodsLableInp2" style="display: none" class="goodsLableRemove2 clearfix pl-10 mb-5"><div class="pd-5"><input name="label02" type="radio" value="" class="radio" /><label class="pl-10" id="goodsLabel2"></label></div><div class="class-input"><input type="text" autofocus class="input-text radius f-l firstLableId2" maxlength="15" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addLableClass(2,\'' + labelId + '\',\'.firstLableId2\')" class="ml-10">确认</a><a href="##" onclick="celLableFirstAdd(\'#goodsLableInp2\')" class="ml-10">取消</a></div></div></li>')
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#goodsLabel2").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200)
                    $("#goodsLableInp2").css({
                        display:"block"
                    })
                }
            } else {
                $("#lableSet2").append('<li id="goodsLableInp2" style="display: none" class="goodsLableRemove2 clearfix pl-10 mb-5"><div class="pd-5"><input name="label02" type="radio" value="" class="radio" /><label class="pl-10" id="goodsLabel2"></label></div><div class="class-input"><input type="text" autofocus class="input-text radius f-l firstLableId2" maxlength="15" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addLableClass(2,\'' + labelId + '\',\'.firstLableId2\')" class="ml-10">确认</a><a href="##" onclick="celLableFirstAdd(\'#goodsLableInp2\')" class="ml-10">取消</a></div></div></li>')
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#goodsLabel2").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200)
                    $("#goodsLableInp2").css({
                        display:"block"
                    })
                }
                for(var i = 0; i < data.data.length; i++) {
/*
                    $("#lableSet2").append('<li class="goodsLableRemove2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input  name="label02" type="radio" value="' + data.data[i].lable_id + '" class="radio" /><label class="pl-10">' + data.data[i].lable_code + ' <input title="' + data.data[i].lable_name + '" type="text" value="' + data.data[i].lable_name + '" disabled style="height: 24px;width: 145px;border: 0px;background: white;margin-top: -4px"></label></div><div class="f-r"><a href="##" onclick="eidtLableInp(\'#goodsLableInp2\',\'#goodsLabel2\',' + data.data[i].lable_code + ',\'.firstLableId2\',\'' + data.data[i].lable_name + '\',\'' + data.data[i].lable_id + '\')" class="Hui-iconfont icon-edit mr-10 powerButton 409">&#xe6df;</a><a href="##" onclick="deleteLableClass(' + data.data[i].lable_id + ',2,'+labelId+')" class="Hui-iconfont icon-edit mr-10 powerButton 410">&#xe6e2;</a></div></div></li>')
*/
                    //无删除编辑按钮
                    //只展示启用状态下
                    if(data.data[i].status==0){
                        $("#lableSet2").append('<li class="goodsLableRemove2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input  name="label02" type="radio" value="' + data.data[i].lable_id + '" class="radio" /><label class="pl-10"><input title="' + data.data[i].lable_name + '" type="text" value="' + data.data[i].lable_name + '" disabled style="height: 24px;width: 145px;border: 0px;background: white;margin-top: -4px"></label></div></div></li>')

                    }

                }

            }
        }
        power();
    };
    return web.doAjax(root.goodsInter.goods_listSubLable, 'post', user_data, succCallBack);
}

function celLableFirstAdd(classId) {
    $(classId).css({
        display: "none"
    })
}

/!*添加一级场景分类*!/
function addLabelInp(thisid ,classId, labelNum, lableLevel, lableCode, lableId) {
    $(".firstLableId1").css({
        border: "1px #ddd solid"
    })
    $(".msgError").html("")
    if(labelNum == 2){
        thisid.parentElement.parentElement.firstChild.firstChild.checked = "checked";//点击加号出发单选框
        goods_listLable2(2,thisid.parentElement.parentElement.firstChild.firstChild.value,true);
    }
    var container = $('.bk-gray');
    $(container).scrollTop(0);
    $(".firstLableId1").val("");
    var codes = document.getElementsByClassName('lablecode');
    var code = codes[codes.length - 1].innerHTML;
    sessionStorage.setItem("isGoodsLableEdit", false);
    // sessionStorage.setItem("lableId", lableId);
    $(classId).css({
        display: "block"
    });
    $(classId).children().children("input").focus();
    var user_data = {
        "lableLevel": lableLevel,
        "lableCode": lableCode,
        "labelId": lableId,
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            setTimeout(function () {
                sessionStorage.setItem('goods_addClass', JSON.stringify(data.data.lableCode));
            },200)
            $("#goodsLabel" + labelNum + "").html(data.data.lableCode);
        } else {
            layer.msg(data.errorMessage, {
                icon: 5,
                time: 1000
            })
        }
        power();
    };
    return web.doAjax(root.goodsInter.goods_getLableCode, 'post', user_data, succCallBack);
}

function eidtLableInp(className, labelName, lablecode, inpName, laNameVal, lableId) {
    $(".inpName").css({
        border: "1px #ddd solid"
    })
    $(".msgError").html("")
    sessionStorage.setItem("isGoodsLableEdit", true);
    sessionStorage.setItem("lableId", lableId);
    $(className).css({
        display: "block"
    });
    $(className).children().children("input").focus();
    $(labelName).html(lablecode);
    $(inpName).val(laNameVal);
}

function celClassFirstAdd(classId) {
    layer.confirm('确定要取消吗？', function(index) {
        layer.msg('已取消', {
            icon: 1,
            time: 1000 //2秒关闭（如果不配置，默认是3秒）
        }, function() {
            $(classId).css({
                display: "none"
            });
            layer.close(index);
        });

    });
}
//获取用户信息
var localStorageParameters = decodeURIComponent(web.readCookie("loginData"));
var JsonObject =JSON.parse(localStorageParameters);
function addLableClass(lableLevel, parentLableId, firstClassId) {
    // 验证场景标签
    /*var flag = validClass(firstClassId);
    if(flag == false) {
        return;
    }*/
    var isGoodsLableEdit = sessionStorage.getItem("isGoodsLableEdit");
    if(isGoodsLableEdit == "true") {
        if($(firstClassId).val() == "") {
            $(firstClassId).css({
                border: "1px red solid"
            })
            $(".msgError").html("<img src='../../lib/Validform/images/error.png'>分类名称不能为空")
        }else{
            if($(firstClassId).val().length > 15){
                $(firstClassId).css({
                    border: "1px red solid"
                })
                $(".msgError").html("<img src='../../lib/Validform/images/error.png'>格式错误请重新输入")
            }else {
                var user_data = JSON.stringify({
                    lableId: sessionStorage.getItem("lableId"),
                    lableName: $(firstClassId).val(),
                    lastModifiedBy:JsonObject.user.user_ID,
                });
                $.ajax({
                    type: 'POST',
                    url: root.goodsInter.goods_updateLable,
                    data: user_data,
                    dataType: 'json',
                    contentType: "application/json",
                    success: function(data) {
                        if(data.errorCode == 0) {
                            layer.msg("编辑成功", {
                                icon: 6,
                                time: 1000
                            }, function() {
                                if(lableLevel == 1){
                                    $("#lableSet1").empty();
                                    goods_listLable1(1);
                                }else {
                                    $("#lableSet2").empty();
                                    goods_listLable2(2,parentLableId)
                                }

                            })
                        } else {
                            layer.msg(data.errorMessage, {
                                icon: 5,
                                time: 1000
                            })
                        }
                    },
                });
            }
        }
    } else {
        if($(firstClassId).val() == "") {
            $(firstClassId).css({
                border: "1px red solid"
            })
            $(".msgError").html("<img src='../../lib/Validform/images/error.png'>分类名称不能为空")
        }else{
            if($(firstClassId).val().length > 15){
                $(firstClassId).css({
                    border: "1px red solid"
                })
                $(".msgError").html("<img src='../../lib/Validform/images/error.png'>格式错误请重新输入")
            }else {
                var classCodes = JSON.parse(sessionStorage.getItem("goods_addClass"));
                var user_data = JSON.stringify({
                    "lableLevel": lableLevel,
                    "lableCode": classCodes.lableCode,
                    "code": classCodes.code,
                    "lableName": $(firstClassId).val(),
                    "parentLableId": parentLableId,
                    "creationBy":JsonObject.user.user_ID,
                    "platformType":20
                });
                $.ajax({
                    type: 'POST',
                    url: root.goodsInter.goods_addLable,
                    data: user_data,
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function(data) {
                        console.log(data)
                        console.log(data)
                        if(data.errorCode == 0) {
                            layer.msg("添加成功", {
                                icon: 6,
                                time: 1000
                            }, function() {
                                console.log(1111)
                                if(lableLevel == 1){
                                    $("#lableSet1").empty();
                                    goods_listLable1(1);
                                }else {
                                    $("#lableSet2").empty();
                                    goods_listLable2(2,parentLableId)
                                }
                            })
                        } else {
                            layer.msg(data.errorMessage, {
                                icon: 5,
                                time: 1000
                            })
                        }

                    },
                });
            }
        }
    }
}

/*删除场景*/
function deleteLableClass(classId,lableLevel,parentLableId) {
    layer.confirm('确认要删除吗？', function(index) {
        var class_data = {
            lableId: classId
        }
        var succCallBack = function(data, status, response) {
            console.log(data)
            if(data.errorCode == "0") {
                layer.msg("删除成功", {
                    icon: 6,
                    time: 1000
                }, function() {
                    if(lableLevel == 1){
                        $("#lableSet1").empty();
                        goods_listLable1(1);
                    }else {
                        $("#lableSet2").empty();
                        goods_listLable2(2,parentLableId)
                    }
                })
            } else {
                layer.msg(data.errorMessage, {
                    icon: 5,
                    time: 1000
                }, function() {
                    if(lableLevel == 1){
                        $("#lableSet1").empty();
                        goods_listLable1(1);
                    }else {
                        $("#lableSet2").empty();
                        goods_listLable2(2,parentLableId)
                    }
                })
            }
            power();
        };
        return web.doAjax(root.goodsInter.goods_delLable, 'post', class_data, succCallBack,1);
    });
}

/*场景标签管理----------------end-----------------------------------------------------------------------------------------------*/